package com.sogoodsoft.web.crm;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.sogoodsoft.entity.account.User;
import com.sogoodsoft.entity.app.Organization;
import com.sogoodsoft.entity.app.SysClass;
import com.sogoodsoft.entity.crm.DocFile;
import com.sogoodsoft.entity.crm.Document;
import com.sogoodsoft.service.app.OrganizationService;
import com.sogoodsoft.service.app.SysClassService;
import com.sogoodsoft.service.common.AbstractService;
import com.sogoodsoft.service.crm.DocFileService;
import com.sogoodsoft.service.crm.DocumentService;
import com.sogoodsoft.util.DwzMsg;
import com.sogoodsoft.util.InfosysSecurityUtil;
import com.sogoodsoft.web.BaseAction;

@Results({ @Result(name = "view", location = "/WEB-INF/content/crm/document-view.jsp"),
        @Result(name = "viewDetail", location = "/WEB-INF/content/crm/document-viewDetail.jsp"),
        @Result(name = "UPLOAD", location = "documentOver.jsp") })
public class DocumentAction extends BaseAction < Document, Long > {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Autowired
    private DocumentService documentService;

    @Autowired
    private DocFileService docFileService;

    @Autowired
    private SysClassService sysClassService;
    
    @Autowired
    private OrganizationService orgService;

    /** 文档分类 **/
    private Long categoryid;

    /** 私密类型 **/
    private Long doctypeid;
    
    /** 所在部门**/
    private Long departid;

    /** 附件 **/
    private List < File > file;

    /** 文件类型 **/
    private List < String > fileContentType;

    /** 文件名 **/
    private List < String > fileFileName;

    private List < DocFile > docFileList;

    private int state = 300;
    
    private boolean editable;

    public Long getCategoryid() {
        return categoryid;
    }

    public void setCategoryid(Long categoryid) {
        this.categoryid = categoryid;
    }

    public Long getDoctypeid() {
        return doctypeid;
    }

    public void setDoctypeid(Long doctypeid) {
        this.doctypeid = doctypeid;
    }

    public Long getDepartid() {
        return departid;
    }

    public void setDepartid(Long departid) {
        this.departid = departid;
    }

    public List < File > getFile() {
        return file;
    }

    public void setFile(List < File > file) {
        this.file = file;
    }

    public List < String > getFileContentType() {
        return fileContentType;
    }

    public void setFileContentType(List < String > fileContentType) {
        this.fileContentType = fileContentType;
    }

    public List < String > getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(List < String > fileFileName) {
        this.fileFileName = fileFileName;
    }

    public List < DocFile > getDocFileList() {
        return docFileList;
    }

    public void setDocFileList(List < DocFile > docFileList) {
        this.docFileList = docFileList;
    }
    
    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    @SuppressWarnings("rawtypes")
    @Override
    protected AbstractService getService() {
        return documentService;
    }

    @Override
    public String save() throws Exception {
       // User user = InfosysSecurityUtil.getCurrentUser();
        Date d = new Date();
        SysClass sc = sysClassService.get(categoryid);
        entity.setCategory(sc);

        sc = sysClassService.get(doctypeid);
        entity.setDoctype(sc);
        
        Organization org = orgService.get(departid);
        entity.setDepart(org);
        
        //entity.setCreator(user.getName());
        //entity.setCreatorid(user.getId());
        entity.setLastupdated(d);
        documentService.save(entity);
        documentService.saveFile(file, fileFileName, fileContentType, entity);
        // Struts2Utils.renderJson(DwzMsg.successAndX(Struts2Utils.getParameter(DwzMsg.param_navTabId)));
        state = 200;
        return "UPLOAD";
    }

    /**
     * 文档查看列表
     * 
     * @return
     * @throws Exception
     */
    public String view() throws Exception {
        this.list();
        return "view";
    }

    /**
     * 文档详情查看
     * 
     * @return
     * @throws Exception
     */
    public String viewDetail() throws Exception {
        entity = documentService.get(id);
        docFileList = docFileService.getDocFiles(id);
        return "viewDetail";
    }

	
    @Override
	public String input() throws Exception {
    	editable = true;
    	if(id!=null&&!InfosysSecurityUtil.isAdmin()){
    		entity = documentService.get(id);
    		User user = InfosysSecurityUtil.getCurrentUser();	
    		if(user.getId().compareTo(entity.getCreatorid())!=0){    			
    			 editable = false;
    		}
    	}
		return INPUT;
	}

	/* 
     * 非管理员，不是自己创建的，不能删除
     */
    @Override
    public String delete() throws Exception {
    	if(!InfosysSecurityUtil.isAdmin()){
    		entity = documentService.get(id);
    		User user = InfosysSecurityUtil.getCurrentUser();	
    		if(user.getId().compareTo(entity.getCreatorid())!=0){
    			 Struts2Utils.renderJson(DwzMsg.fail("对不起，不能删除别人创建的文档！"));
    			 return null;
    		}
    	}
        docFileService.deleteByDocId(id);
        documentService.delete(id);
        Struts2Utils.renderJson(DwzMsg.success());
        return null;
    }

	/* 
	 * 如果没有"查看下载绝密文档"权限，则在列表中过滤掉绝密文档
	 * 如果不是管理员，过滤掉私有
	 */
	@SuppressWarnings("unchecked")
	@Override
	public String list() throws Exception {
		 if(InfosysSecurityUtil.isAdmin()){
			 //管理员
			 page = getService().keyQuery("list.document.admin", page, Struts2Utils.getRequest().getParameterMap());
		 }else{
			 Map<String, String> map = new HashMap<String, String>();
			 map.putAll(Struts2Utils.getRequest().getParameterMap());
			 User user = InfosysSecurityUtil.getCurrentUser();			 
			 map.put("creatorid",String.valueOf(user.getId()));
			 if(InfosysSecurityUtil.hasAuthority("查看下载绝密文档")){
				 map.put("secrettype", "DOC_PRIVATE_SECRET");
			 }
			 
			 page = getService().keyQuery("list.document.user", page, map);
		 }
	
	        logger.debug("查询列表");

	        return SUCCESS;
	}

	public boolean isEditable() {
		return editable;
	}

	public void setEditable(boolean editable) {
		this.editable = editable;
	}
    
    
    
}
